// Utils for handling Dates in Typescript/JavaScript
// Written by Adrian Ehrsam


export module DateUtils {
    export function getFromMSString(date: string): Date {
        var sDate = date.replace("\/Date(", "").replace(")\/", "");
        return new Date(parseInt(sDate));
    };
    function getDatePart(date: Date, format: string): string {
        if (typeof date == "string")
            date = getFromMSString(<string><any>date);
        switch (format) {
            case "yyyy":
                return date.getFullYear().toString();
            case "yy":
                return date.getFullYear().toString().substr(2, 2);
            case "MM":
                var month = date.getMonth() + 1;
                return (month >= 10 ? "" : "0") + month.toString();
            case "M":
                return (date.getMonth() + 1).toString();
            case "dd":
                return (date.getDate() >= 10 ? "" : "0") + date.getDate().toString();
            case "d":
                return date.getDate().toString();
            case "HH":
                var hour = date.getHours();
                return (hour >= 10 ? "" : "0") + hour.toString();
            case "H":
                return date.getHours().toString();
            case "mm":
                var minute = date.getMinutes();
                return (minute >= 10 ? "" : "0") + minute.toString();
            case "m":
                return date.getMinutes().toString();
            case "ss":
                var sec = date.getSeconds();
                return (sec >= 10 ? "" : "0") + sec.toString();
            case "s":
                return date.getSeconds().toString();
            default:
                return format;
        }
    };
    export function formatDate(date: Date, format: string): string {
        if (format == null || date == null)
            return "";
        if (typeof date == "string")
            date = getFromMSString(<string><any>date);
        var originalString: string = format;
        var currentlyParsed: string = null;
        var lastChar: string = null;
        var isInText: bool = false;
        var output: string = "";
        var separatorChars = [" ", "-", ".", ":", "_"];
        var isSeparatorChar = function (input: string): bool {
            for (var i = 0; i < separatorChars.length; i++) {
                if (input == separatorChars[i])
                    return true;
            }
            return false;
        }
        for (var i = 0; i < format.length; i++) {
            var curChar = format.charAt(i);
            var isSepChar = isSeparatorChar(curChar);
            if (isInText) {
                if (curChar == "\"") {
                    isInText = false;
                    currentlyParsed = null;
                }
                else
                    output += curChar;
            }
            else if (curChar == "\"") {
                isInText = true;
            }
            else if (currentlyParsed == null) {
                currentlyParsed = curChar;
            }
            else if (isSepChar || curChar != lastChar || i == format.length - 1) {
                if (!isSepChar) {
                    currentlyParsed += curChar;
                }
                output += getDatePart(date, currentlyParsed);
                currentlyParsed = null;
                if (isSepChar) {
                    output += curChar;
                }
            }
            else {
                currentlyParsed += curChar;
            }
            lastChar = curChar;
        }
        return output;
    };
    export function formatGerman(input: Date): string {
        if (typeof input == "string")
            input = getFromMSString(<string><any>input);
        return formatDate(input, "dd.MM.yyyy");
    };
    export function formatISO(input: Date): string {
        if (typeof input == "string")
            input = getFromMSString(<string><any>input);
        return formatDate(input, "yyyy-MM-dd");
    };
    export function parseISO(input: string): Date {
        if (input == null || input == "")
            return null;
        var parts = input.split("-");
        var year = parseInt(parts[0]);
        var month = parseInt(parts[1]);
        var day = parseInt(parts[2]);
        return new Date(year, month - 1, day);
    };
    export function parseGerman(input: string): Date {
        if (input == null || input == "")
            return null;
        var parts = input.split(".");
        var year = parseInt(parts[2]);
        var month = parseInt(parts[1]);
        var day = parseInt(parts[0]);
        return new Date(year, month - 1, day);
    };
    export function parseDate(input: string): Date {
        if (input == null || input == "")
            return null;
        if (input.indexOf(".") !== -1)
            return parseGerman(input);
        else
            return parseISO(input);
    }
}